package cn.wanda.bi.sunshine.platform.server.service.statistics;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.A06727AppPubSelectedHomepageStatMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppPubSelectedHomepageStat;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppPubSelectedHomepageStatExample;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by James on 17/7/17.
 */
@Service
public class A06727AppPubSelectedHomepageStatService {

    private Logger logger = LoggerFactory.getLogger(A06727AppPubSelectedHomepageStatService.class);

    @Autowired
    private A06727AppPubSelectedHomepageStatMapper a06727AppPubSelectedHomepageStatMapper;

    @Cacheable(value="cache:statistics", key = "'cache:statistics:pubselectedhomepagestat:starttime:'+#startTime+':endtime:'+#endTime+':index:'+#index+':limit:'+#limit")
    public ListResponse list(Long endTime, Long startTime, Integer index, Integer limit) {
        A06727AppPubSelectedHomepageStatExample a06727AppPubSelectedHomepageStatExample = new A06727AppPubSelectedHomepageStatExample();
        A06727AppPubSelectedHomepageStatExample.Criteria criteria = a06727AppPubSelectedHomepageStatExample.createCriteria();
        if (endTime != null && StringUtils.isNotEmpty(endTime + ""))
            criteria.andDatekeyGreaterThanOrEqualTo(new Date(endTime));
        if (startTime != null && StringUtils.isNotEmpty(startTime + ""))
            criteria.andDatekeyLessThanOrEqualTo(new Date(startTime));
        int count = a06727AppPubSelectedHomepageStatMapper.countByExample(a06727AppPubSelectedHomepageStatExample);

        a06727AppPubSelectedHomepageStatExample.setOffset(index * limit - limit);
        a06727AppPubSelectedHomepageStatExample.setLimit(limit);
        a06727AppPubSelectedHomepageStatExample.setOrderByClause("datekey desc");
        List<A06727AppPubSelectedHomepageStat> a06727AppPubHomePageStatList = a06727AppPubSelectedHomepageStatMapper.selectByExample(a06727AppPubSelectedHomepageStatExample);
        return ListResponse.success(a06727AppPubHomePageStatList, count, index, limit);
    }


}
